<script setup>
import {Delete, Edit} from "@element-plus/icons-vue";
import { ref } from 'vue'
import ChannelSelect from "@/views/article/components/ChannelSelect.vue";
import { artGetListService, artDelService } from '@/api/article.js'
import { formatTime } from '@/utils/format.js'
import ArticleEdit from "@/views/article/components/ArticleEdit.vue";
const loading = ref(false)
const total = ref(0)
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 表格数据
const articleList = ref([
  {
    cate_id: 1001,
    pub_date: '2016-05-03',
    title: '小米王总被裁了',
    cate_name: 'Tom',
    state: '未发布',
  },
  {
    cate_id: 1002,
    pub_date: '2016-05-02',
    title: '小米王总被裁了',
    cate_name: 'Tom',
    state: '已发布',
  },
  {
    cate_id: 1003,
    pub_date: '2016-05-04',
    title: '小米王总被裁了',
    cate_name: 'Tom',
    state: '已发布',
  },
  {
    cate_id: 1004,
    pub_date: '2016-05-01',
    title: '小米王总被裁了',
    cate_name: 'Tom',
    state: '已发布',
  },
])
// 获取文章管理列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  loading.value = false
  articleList.value = res.data.data
  total.value = res.data.total
}
// getArticleList()

// 删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  // await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  // getArticleList()
}

// 搜索
const onSearch = () => {
  params.value.pagenum = 1
  // getArticleList()
}
// 重置
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  // getArticleList()
}

/*
* 每页多少条
* */
const handleSizeChange = (val) => {
  params.value.pagenum = 1
  params.value.pagesize = val
  // getArticleList()
}
/*
* 页数
* */
const handleCurrentChange = (val) => {
  params.value.pagenum = val
  getArticleList()
}

/*
* 发布文章
* */
const articleEditRef = ref(null)
const onAddArticle = () => {
  articleEditRef.value.open({})
}
/*
* 修改文章
* */
const onEditChannel = (row) => {
  articleEditRef.value.open(row)
}

/*
* 添加或者编辑的回调
* */
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加, 最好渲染最后一项
    const lastPage = Math.ceil((total + 1) / params.value.pagesize)
    // 更新最大页码数
    params.value.pagenum = lastPage
    // getArticleList()
  }
  // 如果是编辑, 直接渲染当前页面
  // getArticleList()
}
</script>

<template>
  <page-container :title="'文章管理'">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <div>
      <el-form inline>
        <el-form-item label="文章分类：">
          <channel-select v-model="params.cate_id" width="240px"></channel-select>
        </el-form-item>
        <el-form-item label="发布状态：">
          <el-select style="width: 240px" v-model="params.state">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table  v-loading="loading" :data="articleList" style="width: 100%">
        <template #empty>
          <el-empty description="无数据" />
        </template>
        <el-table-column prop="title" label="文章标题">
          <template #default="{ row }">
            <el-link type="primary" :underline="false">{{ row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="分类"  />
        <el-table-column prop="pub_date" label="发布时间">
          <template #default="{ row }">
            {{ formatTime(row.pub_date)}}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" />
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              :icon="Edit"
              circle
              plain
              type="primary"
              @click="onEditChannel(row)"
            ></el-button>
            <el-button
              :icon="Delete"
              circle
              plain
              type="danger"
              @click="onDelChannel(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>

      </div>
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 4, 5, 10]"
        background
        layout="jumper, total, prev, pager, next, sizes"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </div>
  </page-container>
<!--  抽屉-->
  <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
</template>

<style scoped>

</style>
